//
//  NetworkTools.swift
//  Alamofire的测试
//
//  Created by 1 on 16/9/19.
//  Copyright © 2016年 小码哥. All rights reserved.
//

import UIKit
import Alamofire

enum MethodType {
    case get
    case post
}

class NetworkTools {
    //第一部分推荐数据
    class func getRecommandOneData() -> String{
        let strPath = Bundle.main.path(forResource: "HomeOne", ofType: "json")
        guard let newStrPath = strPath else { return ""}
        
        do {
            let jsonStr = try String.init(contentsOf: URL.init(fileURLWithPath: newStrPath))
            // 4.将结果回调出去
            return jsonStr
        } catch {
            return ""
        }
    }
    
    //第二部分推荐数据
    class func getRecommandTwoData() -> String{
        let strPath = Bundle.main.path(forResource: "HomeTwo", ofType: "json")
        guard let newStrPath = strPath else { return ""}
        
        do {
            let jsonStr = try String.init(contentsOf: URL.init(fileURLWithPath: newStrPath))
            // 4.将结果回调出去
            return jsonStr
        } catch {
            return ""
        }
    }
    
    //第三部分推荐数据
    class func getRecommandThreeData() -> String{
        let strPath = Bundle.main.path(forResource: "HomeThree", ofType: "json")
        guard let newStrPath = strPath else { return ""}
        
        do {
            let jsonStr = try String.init(contentsOf: URL.init(fileURLWithPath: newStrPath))
            // 4.将结果回调出去
            return jsonStr
        } catch {
            return ""
        }
    }
    
    // 请求无线轮播的数据
    class func getCycleData() -> String{
        let strPath = Bundle.main.path(forResource: "HomeCycle", ofType: "json")
        guard let newStrPath = strPath else { return ""}
        
        do {
            let jsonStr = try String.init(contentsOf: URL.init(fileURLWithPath: newStrPath))
            // 4.将结果回调出去
            return jsonStr
        } catch {
            return ""
        }
    }
    
    
    // 请求游戏的数据
    class func getGameData() -> String{
        let strPath = Bundle.main.path(forResource: "Game", ofType: "json")
        guard let newStrPath = strPath else { return ""}
        
        do {
            let jsonStr = try String.init(contentsOf: URL.init(fileURLWithPath: newStrPath))
            // 4.将结果回调出去
            return jsonStr
        } catch {
            return ""
        }
    }
    
    
    // 请求娱乐的数据
    class func getAmuseData() -> String{
        let strPath = Bundle.main.path(forResource: "Amuse", ofType: "json")
        guard let newStrPath = strPath else { return ""}
        
        do {
            let jsonStr = try String.init(contentsOf: URL.init(fileURLWithPath: newStrPath))
            // 4.将结果回调出去
            return jsonStr
        } catch {
            return ""
        }
    }
    
    
    //趣玩数据
    class func getQuWanData() -> String{
        let strPath = Bundle.main.path(forResource: "Funny", ofType: "json")
        guard let newStrPath = strPath else { return ""}
        
        do {
            let jsonStr = try String.init(contentsOf: URL.init(fileURLWithPath: newStrPath))
            // 4.将结果回调出去
            return jsonStr
        } catch {
            return ""
        }
    }
    
    class func requestData(_ type : MethodType, URLString : String, parameters : [String : Any]? = nil, finishedCallback :  @escaping (_ result : Any) -> ()) {
    
        // 发送网络请求
        Alamofire.request("https://www.baidu.com", method: HTTPMethod.get, parameters: nil).responseJSON { (response) in
            
            var result:String = ""
            //这里模拟每一个接口   并把数据返回
            if URLString.hasPrefix("http://capi.douyucdn.cn/api/v1/getbigDataRoom") {
                //请求第一部分推荐数据
                result = self.getRecommandOneData()
            }else if URLString.hasPrefix("http://capi.douyucdn.cn/api/v1/getVerticalRoom") {
                //请求第二部分颜值数据
                result = self.getRecommandTwoData()
            }else if URLString.hasPrefix("http://capi.douyucdn.cn/api/v1/getHotCate"){
                //请求2-12部分游戏数据
                result = self.getRecommandThreeData()
            }else if URLString.hasPrefix("http://www.douyutv.com/api/v1/slide/6"){
                // 请求无线轮播的数据
                result = self.getCycleData()
            }else if URLString.hasPrefix("http://capi.douyucdn.cn/api/v1/getColumnDetail") {
                //游戏数据
                result = self.getGameData()
            }else if URLString.hasPrefix("http://capi.douyucdn.cn/api/v1/getHotRoom/2") {
                //娱乐数据
                result = self.getAmuseData()
            }else if URLString.hasPrefix("http://capi.douyucdn.cn/api/v1/getColumnRoom/3") {
                //趣玩数据
                result = self.getQuWanData()
            }
            
            
            let jsonData:Data = result.data(using: .utf8)!
            let dict = try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers)
            guard let resultDict = dict else { return }
            finishedCallback(resultDict as! NSDictionary)
        }
        return
        
        
        
        
        
        
        
        // 1.获取类型
        let method = type == .get ? HTTPMethod.get : HTTPMethod.post
        
        // 2.发送网络请求
        Alamofire.request(URLString, method: method, parameters: parameters).responseJSON { (response) in
            
            // 3.获取结果
            guard let result = response.result.value else {
                print(response.result.error ?? "no data")
                return
            }
            
            // 4.将结果回调出去
            finishedCallback(result)
        }
    }
}


